package com.imooc.feeds.mapper;

import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.imooc.commons.model.vo.Feeds;

public interface FeedsMapper {

	//添加feed
	@Insert("insert into t_feeds (content,fk_diner_id,praise_amount, "
			+ "comment_amount,fk_restaurant_id,create_date,update_date,is_valid) "
			+ "values (#{content}, #{fkDinerId}, #{praiseAmount}, #{commentAmount}, ${fkRestaurantId}, "
			+ "now(),now(),1)")
	@Options(useGeneratedKeys = true, keyProperty="id")
	int save(Feeds feeds);

	//查询feed

	@Select("select id,content,fk_diner_id,praise_amount, "
			+ "comment_amount,fk_restaurant_id,create_date,update_date,is_valid "
			+ "from t_feeds where id=#{id} and is_valid =1")
	Feeds findById(@Param("id")Integer id);

	//删除
	@Delete("delete from t_feeds where id =#{id}")
	int delete(@Param("id") Integer id);

	//根据食客id查询feed
	@Select("select id,content,fk_diner_id,praise_amount, "
			+ "comment_amount,fk_restaurant_id,create_date,update_date,is_valid "
			+ "from t_feeds where fk_diner_id = #{dinerId} and is_valid =1")
	List<Feeds> findByDinerId(@Param("dinerId") Integer dinerId);

	// 根据多主键查询Feed
	@Select("<script> "
			+ "select id,content,fk_diner_id,praise_amount,"
			+ "comment_amount,fk_restaurant_id,create_date,update_date,is_valid"
			+ " from t_feeds where is_valid = 1 and id in "
			+ "<foreach item=\"id\" collection=\"feedIds\" open=\"(\" separator=\",\" close=\")\"> "
			+ "#{id} "
			+ "</foreach> order by id desc "
			+ "</script>")
	List<Feeds> findFeedsByIds(@Param("feedIds")Set<Integer> feedIds);

}
